// 质数
// 判断质数
export const isPrime = (num) => {
    for (let i = 2; i < Math.sqrt(num); i++) {
        if (num % i === 0) {
            return false
        }
    }
    return true
}

// 寻找质数字 n-m之间
export const getPrime = (n, m) => {
    let plist = []
    for (let i = n; i < m; i++) {
        if (isPrime(i)) {
            plist.push(i)
        }

    }
    return plist
}

// console.log(getPrime(0,200))
//  每N个数的质数个数
export const countPrime = (n, m) => {
    let countList = []
    for (let i = 0; i < m; i++) {
        let plist = getPrime(i * n, (i + 1) * n)
        countList.push(plist.length)
    }
    return countList
}

// let arr=countPrime(100, 100000)
// console.log(arr.slice(arr.length -3))

// 公约数 【最大】
export const gcd = (a, b) => {
    if (b === 0) return a
    return gcd(b, a % b)
}

// 判断互为质数
export const checkPrime = (a, b) => {
    return gcd(a, b) === 1
}

// let g = checkPrime(4, 7)

// console.log(g)

// 分解质因数
export const primeFactorize = (num) => {
    let list = []
    let fn = (num) => {
        let flag = true
        for (let i = 2; i <= Math.sqrt(num); i++) {
            if (num % i === 0) {
                list.push(i)
                fn(num / i)
                flag = false
                break
            }
        }
        if (flag) {
            list.push(num)
        }
    }
    fn(num)
    return list

}

// console.log(primeFactorize(200))
// 判断对称数
export const isSymmetry = (num) => {
    let arr = String(num).split('').reverse()
    if (arr.join('') === String(num)) {
        return true
    }
    return false
}


// console.log(isSymmetry(12112))

// export cosnt getMaxGcd=(a,b)=>{

// }

// console.log(gcd(4,8))
// 最小公倍数least common multiple
// 两个数的乘积等于这两个数的最大公约数与最小公倍数的乘积
export const lcm = (a, b) => {
    return a * b / gcd(a, b)
}

// console.log(gcd(16, 24))
// console.log(lcm(16, 24))

// 多个数的最大公约数
// 先求出两个的最大公因数，然后再用求出来的最大公因数和第三个求，依次类推
// 数组的最大公约数
export const gcd2 = (arr) => {
    if (Array.isArray(arr)) {
        let len = arr.length
        if (len === 2) {
            return gcd(...arr)
        } else if (len > 2) {
            let [a, b, ...rest] = arr
            let g = gcd(a, b)
            return gcd2([g, ...rest])
        }
    }
}



// 多个数的最小公倍数
export const lcm2 = (arr) => {
    if (Array.isArray(arr)) {
        let len = arr.length
        if (len === 2) {
            return lcm(...arr)
        } else if (len > 2) {
            let [a, b, ...rest] = arr
            let g = lcm(a, b)
            return lcm2([g, ...rest])
        }
    }
}

console.log(lcm2([4, 8, 7, 14, 12, 16, 2]))//, 12, 16, 2